// scenic-list.js
import { http } from '../../server/http.server'
import { httpRouter } from '../../server/http.router'
import { _ } from '../../libs/underscore/underscore.modified'
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    spotlist:[],
    loadStatus: true
  },
  onShareAppMessage: function (res) {
    return {
      title: 'Ai导游',
      path: '/pages/index/index',
      success: function (res) { },
      fail: function (res) { }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getSpots(app.globalData.city.CityId);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 
   * 
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  /**
   * 获取景区列表
   */
  getSpots: function (cityId){
    let that = this;
    http.get(httpRouter.spotList(cityId)).subscribe({
      next: res => {
        console.log(res.data.Content.HotSpots);
        that.format(res.data.Content.HotSpots);
      },
      error: err => console.log(err),
      complete: () => console.log('Todos--get completed')
    })
  },
  format:function(data){
    let list = _.map(data, function (item) {
      let distance = app.getDistance(item.Lat, item.Lng);
      item.distance = parseInt(distance);
      return item;
    });

    let partition = _.partition(list,function(item,i){
      return i<5;
    })
    let other = _.sortBy(partition[1], function (item) {
      return item.distance;
    });

    let newlist = _.union(partition[0], other);

    this.setData({
      spotlist: newlist,
      loadStatus:false
    })
  }
})